package com.auxgroup.hr.front.service.hireperson.impl;

import com.auxgroup.hr.front.dao.domain.*;
import com.auxgroup.hr.front.dao.repository.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.service.hireperson.HirePersonService;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author lvliuhao
 * @version 创建时间：2018年5月22日 下午8:57:51
 * 入职人员工作经历service
 */
@Service("hirePersonService")
public class HirePersonServiceImpl implements HirePersonService {
    private static final Logger logger = LoggerFactory.getLogger(HirePersonServiceImpl.class);


    @Autowired
    private PsCHirePerTblRepository psCHirePerTblRepository;
    @Autowired
    private PsCNextNbrTblRepository psCNextNbrTblRepository;
    @Autowired
    private PsRsHireIdTblRepository psRsHireIdTblRepository;
    @Autowired
    private PsCPersonalDataRepository psCPersonalDataRepository;
    @Autowired
    private PsRsBlkListTblRepository psRsBlkListTblRepository;
    @Autowired
    private PsCGpLbabtTRepository psCGpLbabtTRepository;
    @Autowired
    private PsCGpLbCmpttTRepository psCGpLbCmpttTRepository;
    @Autowired
    private PsCGpServpkgTRepository psCGpServpkgTRepository;
    @Autowired
    private PsCGpWkinjuryTRepository psCGpWkinjuryTRepository;


    /* (non-Javadoc)
     * @see com.auxgroup.hr.front.service.HirePersonService#gethirePer(java.lang.String)
     */
    @Override
    public PsCHirePerTbl gethirePer(String hireID) {
        return psCHirePerTblRepository.findOne(hireID);
    }

    @Override
    public PsCHirePerTbl initNewHirePerson(String nationId, String name) {
        PsCHirePerTbl tbl = new PsCHirePerTbl();
        tbl.setNationalId(nationId);
        tbl.setName(name);

        tbl.setFirstName(name.substring(1, name.length()));
        tbl.setLastName(name.substring(0, 1));
        //默认为一线
        tbl.setPlatLx(PsCHirePerTbl.PLAT_YIXIAN);
//		ObjectFieldUtils.handleNullValue(tbl);
        return save(tbl);
    }

    @Override
    public PsCHirePerTbl getByNationalId(String nationId) {
        List<PsCHirePerTbl> ps = psCHirePerTblRepository.findByNationalId(nationId);
        return ps.isEmpty() ? null : ps.get(ps.size() - 1);
    }

    @Override
    public PsCHirePerTbl updateContactInfo(PsCHirePerTbl t) {
        PsCHirePerTbl ps = psCHirePerTblRepository.findOne(t.getRsHireId());
        ps.setPhone(t.getPhone());
        ps.setSecPhone(t.getSecPhone());
        ps.setcFax(t.getcFax());
        ps.setEmailAddr(t.getEmailAddr());
        //公司邮箱
        ps.setcEmailAddr(t.getcEmailAddr());
        ps.setPhone2(t.getPhone2());

        ps.setEmeContract(t.getEmeContract());
        ps.setEmePhone(t.getEmePhone());
        ps.setcRelationFam(t.getcRelationFam());

//		ObjectFieldUtils.handleNullValue(ps);
        return save(ps);
    }

    @Override
    public PsCHirePerTbl updateBaseInfo(PsCHirePerTbl t) {
        PsCHirePerTbl ps = psCHirePerTblRepository.findOne(t.getRsHireId());
        ps.setLastName(t.getLastName());
        ps.setFirstName(t.getFirstName());
        ps.setSex(t.getSex());
        ps.setNameAc(t.getNameAc());
        ps.setCountry(t.getCountry());
        ps.setBirthdate(t.getBirthdate());
        ps.setEthnicGrpCd(t.getEthnicGrpCd());
        ps.setNativePlaceChn(t.getNativePlaceChn());
        ps.setHighestEducLvl(t.getHighestEducLvl());
        ps.setcDegree(t.getcDegree());
        ps.setcMajor(t.getcMajor());
        ps.setcTolType2(t.getcTolType2());
        ps.setcGraType(t.getcGraType());
        ps.setcSchool(t.getcSchool());
        ps.setSchoolOth(t.getSchoolOth());
        ps.setcSchoolSystem(t.getcSchoolSystem());
        ps.setGraEndDt(t.getGraEndDt());
        ps.setSecMajor(t.getSecMajor());
        ps.setZcJszc(t.getZcJszc());
        ps.setcPfqType(t.getcPfqType());
        ps.setStuNum(t.getStuNum());

        ps.setcEmplFrom(t.getcEmplFrom());
        ps.setcEmplSched(t.getcEmplSched());
        ps.setPerTjr(t.getPerTjr());

        ps.setStartDtChn(t.getStartDtChn());
        ps.setMarStatus(t.getMarStatus());
        ps.setPoliticalStaChn(t.getPoliticalStaChn());

        ps.setcHukType(t.getcHukType());
        ps.setJtHukou(t.getJtHukou());
        ps.setNationalIdType(t.getNationalIdType());
        ps.setNationalId(t.getNationalId());

        ps.setNatExperDt(t.getNatExperDt());
        ps.setIsJiazhao(t.getIsJiazhao());
        ps.setJiaZhaoDt1(t.getJiaZhaoDt1());
        ps.setJiaZhaoDt2(t.getJiaZhaoDt2());
        ps.setXingshizDt(t.getXingshizDt());
        ps.setPerDabh(t.getPerDabh());
        //户籍地址
        ps.setAddresslong1(t.getAddresslong1());
        ps.setYn(t.getYn());
        ps.setPerRhdt(t.getPerRhdt());
        //家庭住址
        ps.setAddresslong3(t.getAddresslong3());
        ps.setYn2(t.getYn2());
        ps.setYn3(t.getYn3());
        ps.setAddresslong2(t.getAddresslong2());

//
//		ObjectFieldUtils.handleNullValue(ps);
//		if(ps.isComplete()) {
//			ps.setRsCompleteFlg("C");
//		}
        return save(ps);
    }

    /* (non-Javadoc)
     * @see com.auxgroup.hr.front.service.HirePersonService#save(PsCHirePerTbl)
     */
    @Override
    public PsCHirePerTbl save(PsCHirePerTbl psCHirePerTbl) {
        boolean isNew = false;
        if (psCHirePerTbl.isComplete()) {
            psCHirePerTbl.setRsCompleteFlg("C");//必填校验通过
        } else {
            psCHirePerTbl.setRsCompleteFlg("W");
        }
        if (StringUtils.isEmpty(psCHirePerTbl.getRsHireId())) {
            isNew = true;
            psRsHireIdTblRepository.increase();
            String maxHireId = psRsHireIdTblRepository.getMaxHireId();
            BigDecimal newHireId = new BigDecimal(maxHireId);//.add(BigDecimal.ONE);
            if (newHireId.compareTo(new BigDecimal(10000000)) < 0) {
                newHireId = newHireId.add(new BigDecimal(10000000));
            }
            psCHirePerTbl.setRsHireId(newHireId.toString());
            psCHirePerTbl.setPlatLx(PsCHirePerTbl.PLAT_YIXIAN);

            psCHirePerTbl.setBlacklist("N");
            psCHirePerTbl.setReEmployee("N");
            psCHirePerTbl.setResignCom("N");
            psCHirePerTbl.setLaborAtri("N");
            psCHirePerTbl.setCompensa("N");
            psCHirePerTbl.setRestricJob("N");
            PsCPersonalData pd = psCPersonalDataRepository.findFirstByNationalId(psCHirePerTbl.getNationalId());
            if (pd != null) {
                logger.info("PsCPersonalData exist:" + pd.getEmplid());
                psCHirePerTbl.setReEmployee("Y");
                PsRsBlkListTbl blk = psRsBlkListTblRepository.findFirstByEmplidAndRsBlkListStatus(pd.getEmplid(), "A");
                if (blk != null) {
                    logger.info("blk exist:" + pd.getEmplid());
                    psCHirePerTbl.setBlacklist("Y");
                }
                PsCGpServpkgT pcgst = psCGpServpkgTRepository.findOne(pd.getEmplid());
                if (pcgst != null) {
                    psCHirePerTbl.setResignCom("Y");
                }
                PsCGpLbabtT psCGpLbabtT = psCGpLbabtTRepository.findOne(pd.getEmplid());
                if (psCGpLbabtT != null) {
                    psCHirePerTbl.setLaborAtri("Y");
                }
                PsCGpWkinjuryT psCGpWkinjuryT = psCGpWkinjuryTRepository.findOne(pd.getEmplid());
                if (psCGpWkinjuryT != null) {
                    psCHirePerTbl.setCompensa("Y");
                }
                PsCGpLbCmpttT psCGpLbCmpttT = psCGpLbCmpttTRepository.findOne(pd.getEmplid());
                if (psCGpLbCmpttT != null) {
                    psCHirePerTbl.setRestricJob("Y");
                }
            }

        }

        if (StringUtils.isEmpty(psCHirePerTbl.getApplicationId())) {
            psCNextNbrTblRepository.increase();
            String maxApplicationId = psCNextNbrTblRepository.getMaxNumber();
            BigDecimal newApplicationId = new BigDecimal(maxApplicationId);//.add(BigDecimal.ONE);
            if (newApplicationId.compareTo(new BigDecimal(10000000)) < 0) {
                newApplicationId = newApplicationId.add(new BigDecimal(10000000));
            }
            psCHirePerTbl.setApplicationId(newApplicationId.toString());
        }
        if (isNew) {
            List<PsCHirePerTbl> doubleCheck = psCHirePerTblRepository.findByNationalId(psCHirePerTbl.getNationalId());
            if (!doubleCheck.isEmpty()) {
                return doubleCheck.get(doubleCheck.size() - 1);
            }
        }
        if (psCHirePerTbl.getHrsRowAddDttm() == null) {
            psCHirePerTbl.setHrsRowAddDttm(new Timestamp(System.currentTimeMillis()));
        }
        psCHirePerTbl.setRsHirStatus("W");//未入职
        ObjectFieldUtils.handleNullValue(psCHirePerTbl);

        return psCHirePerTblRepository.save(psCHirePerTbl);
    }

    /* (non-Javadoc)
     * @see com.auxgroup.hr.front.service.HirePersonService#delete(java.lang.String)
     */
    @Override
    public Boolean delete(String hireID) {
        try {
            psCHirePerTblRepository.delete(hireID);
        } catch (Exception e) {
            logger.error("delete hirePerson error: hireID={}", hireID, e);
            return false;
        }
        return true;
    }

    @Override
    public PsCHirePerTbl updateEducatInfo(PsCHireEducatT psCHireEducatT) {

        PsCHirePerTbl ps = psCHirePerTblRepository.findOne(psCHireEducatT.getRsHireId());

        ps.setHighestEducLvl(psCHireEducatT.getcEduciton());
        ps.setcDegree(psCHireEducatT.getcDegree());
        ps.setcTolType2(psCHireEducatT.getcTolType2());
        ps.setcSchoolSystem(psCHireEducatT.getcSchoolSystem());
        ps.setGraEndDt(psCHireEducatT.getEndDt());
        ps.setcMajor(psCHireEducatT.getcMajor());
        ps.setcSchool(psCHireEducatT.getcSchool());
        if (psCHireEducatT.getcSchoolOth() != null) {
            ps.setSchoolOth(psCHireEducatT.getcSchoolOth());
        }
        return psCHirePerTblRepository.save(ps);
    }

}
 